package org.algorithm.Backtracking.leetcode46;

import java.util.ArrayList;
import java.util.List;

public class Solution {

    /**
     * 全排列 数组中不含有重复元素
     */
    public List<List<Integer>> permute (int [] nums){
        boolean [] used = new boolean[nums.length];
        backtracking(nums,used);
        return res;
    }

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> item = new ArrayList<>();


    //方式1
    private void backtracking(int [] nums){
        if (item.size()==nums.length){
            res.add(new ArrayList<>(item));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (item.contains(nums[i])){
                continue;
            }
            item.add(nums[i]);
            backtracking(nums);
            item.remove(item.size()-1);
        }
    }

    private void backtracking(int [] nums,boolean [] used){
        if (item.size()==nums.length){
            res.add(new ArrayList<>(item));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (!used[i]){
                item.add(nums[i]);
                used[i]=true;
                backtracking(nums,used);
                used[i]=false;
                item.remove(item.size()-1);
            }
        }
    }
}
